<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends Auth_Controller {

	function __construct() {       
		parent::__construct();
		$this->load->model('user_model','',TRUE);

	}

	public function index()
	{
		$this->data['page_title'] = '首页';
		$this->load->view('home',$this->data);
	}

	public function adminers()
	{
		$this->data['page_title'] = '用户管理';

		$this->data['groups'] = $this->user_model->get_all_groups();

		$this->data['adminers'] = $this->user_model->get_all_users();

		$this->load->view('user/adminer_lists',$this->data);
	}

	public function adminer_add()
	{
		$this->data['page_title'] = '添加用户';

		$this->data['groups'] = $this->user_model->get_all_groups();

		$this->load->view('user/adminer_add',$this->data);
	}

	public function adminer_insert()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$nickname = $this->input->post('nickname');

		$partners = $this->input->post('partner');
		$games = $this->input->post('game');
		$channels = $this->input->post('channel');

		$group = $this->input->post('group');

		if($username&&$password)
		{
			$additional_data = array();
			$additional_data['nickname'] = $nickname;
			$additional_data['group'] = $group;

			$email = 'test@tuo3.com';

			$user_id = $this->ion_auth->register($username, $password, $email, $additional_data);
		
			if(!empty($user_id))
			{
				if($partners)
				{
					foreach ($partners as $key => $partner) {
						$this->db->insert('adminer_partners',array('adminer_id'=>$user_id,'partner_id'=>$partner));
					}
				}	

				if($games)
				{
					foreach ($games as $key => $game) {
						$this->db->insert('adminer_games',array('adminer_id'=>$user_id,'game_id'=>$game));
					}
				}
				if($channels)
				{
					foreach ($channels as $key => $channels) {
						$this->db->insert('adminer_channels',array('adminer_id'=>$user_id,'channel_id'=>$partner));
					}
				}

				// 记录日志
				// $this->auth->insert_user_action_logs('user','adminer_add',$_POST);

				echo json_encode(array('code'=>0,'msg'=>'添加成功'));exit;
			}

			echo json_encode(array('code'=>1,'msg'=>'添加失败'));exit;
		}
			echo json_encode(array('code'=>1,'msg'=>'参数非法'));exit;
	
	}

	public function adminer_update()
	{
		$data = array($_POST['name']=>$_POST['value']);
		$where = array('id'=>$_POST['pk']);
		$this->db->update('users',$data,$where);
		if($this->db->affected_rows()>0)
		{
			ajax_return(0,'更新成功');
		} else {
			ajax_return(1,'更新失败');
		}
	}

	public function adminer_reset_pwd()
	{
		$username = $this->input->post('name');
		$password = $this->input->post('password');

		$this->load->model('ion_auth_model','',TRUE);

		if($username&&$password) {
			if($this->ion_auth_model->reset_password($username,$password))
			{
				ajax_return(0,'更改成功');
			} else {
				ajax_return(1,'更改失败');
			}
		}

	}

	// 删除用户
	public function adminer_delete()
	{
		$user_id = $this->input->post('id');

		if(!empty($user_id))
		{
			//删除用户信息
			$this->user_model->delete_one_user($user_id);
			
			// 记录日志
			// $this->auth->insert_user_action_logs('user','adminer_delete',$_POST);
			ajax_return(0,'删除成功');
		}			
		
	}

	//管理员权限
	public function adminer_power($id)
	{
		$this->load->model('server_model','',TRUE);
		$where = array(
					'adminer_channel.adminer_id'=>$this->ion_auth->get_user_id()
			);
		$this->data['channels'] = $this->server_model->get_all_channels($where);

		$this->load->view('user/adminer_power',$this->data);
	}

	//群组
	public function groups()
	{
		$this->data['page_title'] = '群组管理';		

		$this->data['groups'] = $this->user_model->get_all_groups();

		$this->load->view('user/group_lists',$this->data);
	}

	//增加小组 post
	public function group_add()
	{
		$name = $this->input->post('name');
		$desc = $this->input->post('desc');
		$ret = array();

		if($name&&$desc)
		{
			//check name is exsit?
			if($this->user_model->get_one_group(array('name'=>$name)))
			{
				$ret['code'] = 1;
				$ret['msg'] = '群组名称已经存在';
				echo json_encode($ret);exit;
			}
			
			$data = array('name'=>$name,
				'description'=>$desc,
				'create_time'=>time()
				);

			if($this->user_model->insert_one_group($data))
			{
				// 记录日志
				// $this->auth->insert_user_action_logs('user','group_add',$_POST);

				$ret['code'] = 0;
				$ret['msg'] = '添加成功';
				echo json_encode($ret);
				exit;
			} else {
				$ret['code'] = 1;
				$ret['msg'] = '添加失败';
				echo json_encode($ret);exit;
			}			
		} else {
			$ret['code'] = 1;
			$ret['msg'] = '参数不合法';
			echo json_encode($ret);exit;
		}
	}

	// 更新组信息
	public function group_update()
	{
		$data = array($_POST['name']=>$_POST['value']);
		$where = array('id'=>$_POST['pk']);
		if(!$this->user_model->update_one_group($data,$where))
		{
			ajax_return(1,'更新失败');
		} else {
			ajax_return(0,'更新成功');
		}
	}

	// 删除分组
	public function group_delete()
	{
		$group_id = $this->input->post('id');

		if(!empty($group_id)) {

			$this->user_model->delete_one_group($group_id);

			// 记录日志
			// $this->auth->insert_user_action_logs('user','group_delete',$_POST);

			echo json_encode(array('code'=>0,'msg'=>'删除成功'));exit;
			
		} else {
			echo json_encode(array('code'=>1,'msg'=>'参数非法'));exit;
		}
	}

	// 分组权限编辑
	public function group_permission_edit()
	{
		$group_id = $this->uri->segment(3);

		$this->_data['group_id'] = $group_id;

		//取出角色所有的模块
		$this->_data['g_modules'] = $this->user_model->get_group_modules($group_id);

		$this->_data['g_actions'] = $this->user_model->get_group_actions($group_id);

		//获取所有的父模块
		$father_modules = $this->user_model->get_all_modules();

		$this->_data['father_modules'] = $father_modules;

		foreach ($father_modules as $key=>$module)
		{	
			//获取对应子模块		
			$children_modules = $this->user_model->get_all_modules($module['module_id']);
			foreach($children_modules as $key1=>$module1)
			{
				$children_modules[$key1]['actions'] = $this->user_model->get_module_actions($module1['module_id']);
				// $father_modules[]= $module1;
				// array_splice($father_modules,$key+$key1+1,0,array($module1));
			}

			$father_modules[$key]['childrens'] =  $children_modules;
		}

		$this->_data['modules'] = $father_modules;

		$this->load->view('user/group_permission',$this->_data);
	}

	public function group_permission_update()
	{
		//获取所有的module
		$modules = $this->input->post('module');
		//获取所有的action
		$actions = $this->input->post('action');

		$group = $this->input->post('group');

		if($group)
		{
			//清空原来的权限
			$this->db->delete('group_permissions',array('group_id'=>$group));

			if(!empty($modules)&&is_array($modules)&&count($modules)>0) {
				foreach ($modules as $module) {
					$this->db->insert('group_permissions',array('group_id'=>$group,'permission_id'=>$module,'type'=>'module'));
				}
			}

			if(!empty($actions)&&is_array($actions)&&count($actions)>0) {
				foreach ($actions as $action) {
					$this->db->insert('group_permissions',array('group_id'=>$group,'permission_id'=>$action,'type'=>'action'));
				}
			}

			echo json_encode(array('code'=>0,'msg'=>'更新成功'));
		}

	}

	// 权限管理
	public function modules()
	{
		$this->data['page_title'] = '权限管理';

		//获取所有的父模块
		$father_modules = $this->user_model->get_all_modules();

		$this->data['father_modules'] = $father_modules;

		foreach ($father_modules as $key=>$module)
		{	
			//获取对应子模块		
			$children_modules = $this->user_model->get_all_modules($module['module_id']);
			foreach($children_modules as $key1=>$module1)
			{
				$children_modules[$key1]['actions'] = $this->user_model->get_module_actions($module1['module_id']);
				// $father_modules[]= $module1;
				// array_splice($father_modules,$key+$key1+1,0,array($module1));
			}

			$father_modules[$key]['childrens'] =  $children_modules;
		}

		$this->data['modules'] = $father_modules;

		// echo json_encode($father_modules);exit;

		// $this->data['modules'] = $this->user_model->get_all_modules();

		$this->load->view('user/module_lists',$this->data);		
	}

	//权限添加
	public function module_add()
	{
		$father = $this->input->post('father');
		$name = $this->input->post('name');
		$url = $this->input->post('url');
		$icon = $this->input->post('icon');
		$status = $this->input->post('status');

		$ret = array();

		if($name&&$father!='')
		{
			//检查名称是否存在
			if($this->user_model->get_one_module(array('module_name'=>$name)))
			{
				$ret['code'] = 1;
				$ret['msg'] = '权限名称已经存在';
				echo json_encode($ret);exit;
			}

			//插入
			$data = array('parent_id'=>$father,
				'module_name'=>$name,
				'module_url'=>$url,
				'module_icon'=>$icon,
				'status'=>$status
				);

			if($this->user_model->insert_one_module($data))
			{
				$ret['code'] = 0;
				$ret['msg'] = '添加成功';
				echo json_encode($ret);
			} else {
				$ret['code'] = 1;
				$ret['msg'] = '添加失败';
				echo json_encode($ret);
			}			
		} else {
			$ret['code'] = 1;
			$ret['msg'] = '参数不合法';
			echo json_encode($ret);exit;
		}
	}

	//更新权限
	public function module_update()
	{
		$data = array($_POST['name']=>$_POST['value']);
		$where = array('module_id'=>$_POST['pk']);
		if(!$this->user_model->update_one_module($data,$where))
		{
			ajax_return(1,'更新失败');
		} else {
			ajax_return(0,'更新成功');
		}
	}

	public function module_delete()
	{
		$id = $this->input->post('id');

		if($id)
		{
			$this->db->delete('modules',array('module_id'=>$id));

			if($this->db->affected_rows()>0)
			{
				echo json_encode(array('code'=>0,'msg'=>'删除成功'));exit;
			} else {
				echo json_encode(array('code'=>1,'msg'=>'删除失败'));exit;
			}
		}		
	}


	public function action_add()
	{
		$module = $this->input->post('module');
		$name = $this->input->post('name');
		$icon = $this->input->post('icon');
		$url = $this->input->post('url');

		$status = $this->input->post('status');

		$ret = array();

		if($name&&$module)
		{
			//检查名称是否存在
			if($this->user_model->get_one_action(array('module_id'=>$module,'action_name'=>$name)))
			{
				$ret['code'] = 1;
				$ret['msg'] = '动作名称已经存在';
				echo json_encode($ret);exit;
			}

			//插入
			$data = array('module_id'=>$module,
				'action_name'=>$name,
				'action_url'=>$url,
				'action_icon'=>$icon,
				'status'=>$status
				);

			if($this->user_model->insert_one_action($data))
			{
				$ret['code'] = 0;
				$ret['msg'] = '添加成功';
				echo json_encode($ret);
			} else {
				$ret['code'] = 1;
				$ret['msg'] = '添加失败';
				echo json_encode($ret);
			}			
		} else {
			$ret['code'] = 1;
			$ret['msg'] = '参数不合法';
			echo json_encode($ret);exit;
		}
	}

	public function action_delete()
	{
		$id = $this->input->post('id');

		if($id)
		{
			$this->db->delete('module_actions',array('action_id'=>$id));
			
			if($this->db->affected_rows()>0)
			{
				echo json_encode(array('code'=>0,'msg'=>'删除成功'));exit;
			} else {
				echo json_encode(array('code'=>1,'msg'=>'删除失败'));exit;
			}
		}		
	}

	//更新权限
	public function action_update()
	{
		$data = array($_POST['name']=>$_POST['value']);
		$where = array('action_id'=>$_POST['pk']);
		if(!$this->user_model->update_one_action($data,$where))
		{
			ajax_return(1,'更新失败');
		} else {
			ajax_return(0,'更新成功');
		}
	}

	//操作日志
	public function logs()
	{
		$this->_data['page_title'] = '操作日志';
		$this->_data['logs'] = $this->adminer_model->get_adminer_logs();
		$this->load->view('user/log_lists',$this->_data);
	}


}